/*
 * Copyright (c) 2011-2014, Peter Abeles. All Rights Reserved.
 *
 * This file is part of BoofCV (http://boofcv.org).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package boofcv.alg.misc;

import boofcv.misc.AutoTypeImage;
import boofcv.misc.CodeGeneratorBase;

import java.io.FileNotFoundException;


/**
 * Generates functions inside of {@link boofcv.alg.misc.ImageMiscOps}.
 *
 * @author Peter Abeles
 */
public class GenerateImageMiscOps extends CodeGeneratorBase {

	String className = "ImageMiscOps";

	private AutoTypeImage imageType;
	private String imageName;
	private String dataType;
	private String bitWise;

	public void generate() throws FileNotFoundException {
		printPreamble();
		printAllGeneric();
		printAllSpecific();
		out.println("}");
	}

	private void printPreamble() throws FileNotFoundException {
		setOutputFile(className);
		out.print("import boofcv.struct.image.*;\n" +
				"\n" +
				"import java.util.Random;\n" +
				"\n" +
				"\n" +
				"/**\n" +
				" * Basic image operations which have no place better to go.\n" +
				" *\n" +
				" * <p>DO NOT MODIFY: Generated by {@link "+getClass().getName()+"}.</p>\n"+
				" *\n"+
				" * @author Peter Abeles\n" +
				" */\n" +
				"public class "+className+" {\n\n");
	}

	public void printAllGeneric() {
		AutoTypeImage types[] = AutoTypeImage.getGenericTypes();

		for( AutoTypeImage t : types ) {
			imageType = t;
			imageName = t.getSingleBandName();
			dataType = t.getDataType();
			printCopy();
			printFill();
			printFillInterleaved();
			printFillBorder();
			printFillRectangle();
			printFillUniform();
			printFillUniformInterleaved();
			printFillGaussian();
			printFlipVertical();
			printFlipHorizontal();
			printRotateCW();
			printRotateCCW();
		}
	}

	public void printAllSpecific() {
		AutoTypeImage types[] = AutoTypeImage.getSpecificTypes();

		for( AutoTypeImage t : types ) {
			imageType = t;
			imageName = t.getSingleBandName();
			dataType = t.getDataType();
			bitWise = t.getBitWise();
			printAddUniform();
			printAddGaussian();
		}
	}

	public void printCopy() {
		out.print("\t/**\n" +
				"\t * Copies a rectangular region from one image into another.<br>\n" +
				"\t * output[dstX:(dstX+width) , dstY:(dstY+height-1)] = input[srcX:(srcX+width) , srcY:(srcY+height-1)]\n" +
				"\t *\n" +
				"\t * @param srcX x-coordinate of corner in input image\n" +
				"\t * @param srcY y-coordinate of corner in input image\n" +
				"\t * @param dstX x-coordinate of corner in output image\n" +
				"\t * @param dstY y-coordinate of corner in output image\n" +
				"\t * @param width Width of region to be copied\n" +
				"\t * @param height Height of region to be copied\n" +
				"\t * @param input Input image\n" +
				"\t * @param output output image\n" +
				"\t */\n" +
				"\tpublic static void copy( int srcX , int srcY , int dstX , int dstY , int width , int height ,\n" +
				"\t\t\t\t\t\t\t "+imageName+" input , "+imageName+" output ) {\n" +
				"\n" +
				"\t\tif( input.width < srcX+width || input.height < srcY+height )\n" +
				"\t\t\tthrow new IllegalArgumentException(\"Copy region must be contained input image\");\n" +
				"\t\tif( output.width < dstX+width || output.height < dstY+height )\n" +
				"\t\t\tthrow new IllegalArgumentException(\"Copy region must be contained output image\");\n" +
				"\n" +
				"\t\tfor (int y = 0; y < height; y++) {\n" +
				"\t\t\tint indexSrc = input.startIndex + (srcY + y) * input.stride + srcX;\n" +
				"\t\t\tint indexDst = output.startIndex + (dstY + y) * output.stride + dstX;\n" +
				"\n" +
				"\t\t\tfor (int x = 0; x < width; x++) {\n" +
				"\t\t\t\toutput.data[indexDst++] = input.data[indexSrc++];\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void printFill()
	{
		String typeCast = imageType.getTypeCastFromSum();
		out.print("\t/**\n" +
				"\t * Fills the whole image with the specified value\n" +
				"\t *\n" +
				"\t * @param input An image.\n" +
				"\t * @param value The value that the image is being filled with.\n" +
				"\t */\n" +
				"\tpublic static void fill("+imageName+" input, "+imageType.getSumType()+" value) {\n" +
				"\n" +
				"\t\tfor (int y = 0; y < input.height; y++) {\n" +
				"\t\t\tint index = input.getStartIndex() + y * input.getStride();\n" +
				"\t\t\tfor (int x = 0; x < input.width; x++) {\n" +
				"\t\t\t\tinput.data[index++] = "+typeCast+"value;\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void printFillInterleaved()
	{
		String imageName = imageType.getInterleavedName();
		String typeCast = imageType.getTypeCastFromSum();
		out.print("\t/**\n" +
				"\t * Fills the whole image with the specified value\n" +
				"\t *\n" +
				"\t * @param input An image.\n" +
				"\t * @param value The value that the image is being filled with.\n" +
				"\t */\n" +
				"\tpublic static void fill("+imageName+" input, "+imageType.getSumType()+" value) {\n" +
				"\n" +
				"\t\tfor (int y = 0; y < input.height; y++) {\n" +
				"\t\t\tint index = input.getStartIndex() + y * input.getStride();\n" +
				"\t\t\tint end = index + input.width*input.numBands;\n" +
				"\t\t\tfor (; index < end; index++ ) {\n" +
				"\t\t\t\tinput.data[index] = "+typeCast+"value;\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void printFillBorder()
	{
		String typeCast = imageType.getTypeCastFromSum();

		out.print("\t/**\n" +
				"\t * Fills the outside border with the specified value\n" +
				"\t *\n" +
				"\t * @param input An image.\n" +
				"\t * @param value The value that the image is being filled with.\n" +
				"\t * @param radius Border width.   \n" +
				"\t */\n" +
				"\tpublic static void fillBorder("+imageName+" input, "+imageType.getSumType()+" value, int radius ) {\n" +
				"\n" +
				"\t\t// top and bottom\n" +
				"\t\tfor (int y = 0; y < radius; y++) {\n" +
				"\t\t\tint indexTop = input.startIndex + y * input.stride;\n" +
				"\t\t\tint indexBottom = input.startIndex + (input.height-y-1) * input.stride;\n" +
				"\t\t\tfor (int x = 0; x < input.width; x++) {\n" +
				"\t\t\t\tinput.data[indexTop++] = "+typeCast+"value;\n" +
				"\t\t\t\tinput.data[indexBottom++] = "+typeCast+"value;\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\n" +
				"\t\t// left and right\n" +
				"\t\tint h = input.height-radius;\n" +
				"\t\tint indexStart = input.startIndex + radius*input.stride;\n" +
				"\t\tfor (int x = 0; x < radius; x++) {\n" +
				"\t\t\tint indexLeft = indexStart + x;\n" +
				"\t\t\tint indexRight = indexStart + input.width-1-x;\n" +
				"\t\t\tfor (int y = radius; y < h; y++) {\n" +
				"\t\t\t\tinput.data[indexLeft] = "+typeCast+"value;\n" +
				"\t\t\t\tinput.data[indexRight] = "+typeCast+"value;\n" +
				"\t\t\t\t\n" +
				"\t\t\t\tindexLeft += input.stride;\n" +
				"\t\t\t\tindexRight += input.stride;\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void printFillRectangle()
	{
		out.print("\t/**\n" +
				"\t * Draws a filled rectangle that is aligned along the image axis inside the image.\n" +
				"\t *\n" +
				"\t * @param img Image the rectangle is drawn in.  Modified\n" +
				"\t * @param value Value of the rectangle\n" +
				"\t * @param x0 Top left x-coordinate\n" +
				"\t * @param y0 Top left y-coordinate\n" +
				"\t * @param width Rectangle width\n" +
				"\t * @param height Rectangle height\n" +
				"\t */\n" +
				"\tpublic static void fillRectangle("+imageName+" img, "+imageType.getSumType()+" value, int x0, int y0, int width, int height) {\n" +
				"\t\tint x1 = x0 + width;\n" +
				"\t\tint y1 = y0 + height;\n" +
				"\n" +
				"\t\tfor (int y = y0; y < y1; y++) {\n" +
				"\t\t\tfor (int x = x0; x < x1; x++) {\n" +
				"\t\t\t\tif( img.isInBounds(x,y ))\n" +
				"\t\t\t\t\timg.set(x, y, value);\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void printFillUniform() {

		String sumType = imageType.getSumType();
		String typeCast = imageType.getTypeCastFromSum();
		String maxInclusive = imageType.isInteger() ? "exclusive" : "inclusive";

		out.print("\t/**\n" +
				"\t * Sets each value in the image to a value drawn from an uniform distribution that has a range of min <= X < max.\n" +
				"\t *\n" +
				"\t * @param img Image which is to be filled.  Modified,\n" +
				"\t * @param rand Random number generator\n" +
				"\t * @param min Minimum value of the distribution, inclusive\n" +
				"\t * @param max Maximum value of the distribution, "+maxInclusive+"\n" +
				"\t */\n" +
				"\tpublic static void fillUniform("+imageName+" img, Random rand , "+sumType+" min , "+sumType+" max) {\n" +
				"\t\t"+sumType+" range = max-min;\n" +
				"\n" +
				"\t\t"+dataType+"[] data = img.data;\n" +
				"\n" +
				"\t\tfor (int y = 0; y < img.height; y++) {\n" +
				"\t\t\tint index = img.getStartIndex() + y * img.getStride();\n" +
				"\t\t\tfor (int x = 0; x < img.width; x++) {\n");
		if( imageType.isInteger() && imageType.getNumBits() < 64) {
			out.print("\t\t\t\tdata[index++] = "+typeCast+"(rand.nextInt(range)+min);\n");
		} else if( imageType.isInteger() ) {
			out.print("\t\t\t\tdata[index++] = rand.nextInt((int)range)+min;\n");
		} else {
			String randType = imageType.getRandType();
			out.print("\t\t\t\tdata[index++] = rand.next"+randType+"()*range+min;\n");
		}
		out.print("\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void printFillUniformInterleaved() {

		String imageName = imageType.getInterleavedName();
		String sumType = imageType.getSumType();
		String typeCast = imageType.getTypeCastFromSum();
		String maxInclusive = imageType.isInteger() ? "exclusive" : "inclusive";

		out.print("\t/**\n" +
				"\t * Sets each value in the image to a value drawn from an uniform distribution that has a range of min <= X < max.\n" +
				"\t *\n" +
				"\t * @param img Image which is to be filled.  Modified,\n" +
				"\t * @param rand Random number generator\n" +
				"\t * @param min Minimum value of the distribution, inclusive\n" +
				"\t * @param max Maximum value of the distribution, "+maxInclusive+"\n" +
				"\t */\n" +
				"\tpublic static void fillUniform("+imageName+" img, Random rand , "+sumType+" min , "+sumType+" max) {\n" +
				"\t\t"+sumType+" range = max-min;\n" +
				"\n" +
				"\t\t"+dataType+"[] data = img.data;\n" +
				"\n" +
				"\t\tfor (int y = 0; y < img.height; y++) {\n" +
				"\t\t\tint index = img.getStartIndex() + y * img.getStride();\n" +
				"\t\t\tint end = index + img.width*img.numBands;\n" +
				"\t\t\tfor (; index <  end; index++) {\n");
		if( imageType.isInteger() && imageType.getNumBits() < 64) {
			out.print("\t\t\t\tdata[index] = "+typeCast+"(rand.nextInt(range)+min);\n");
		} else if( imageType.isInteger() ) {
			out.print("\t\t\t\tdata[index] = rand.nextInt((int)range)+min;\n");
		} else {
			String randType = imageType.getRandType();
			out.print("\t\t\t\tdata[index] = rand.next"+randType+"()*range+min;\n");
		}
		out.print("\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void printFillGaussian() {

		String sumType = imageType.getSumType();
		String castToSum = sumType.compareTo("double") == 0 ? "" : "("+sumType+")";
		String typeCast = imageType.getTypeCastFromSum();

		out.print("\t/**\n" +
				"\t * Sets each value in the image to a value drawn from a Gaussian distribution.  A user\n" +
				"\t * specified lower and upper bound is provided to ensure that the values are within a legal\n" +
				"\t * range.  A drawn value outside the allowed range will be set to the closest bound.\n" +
				"\t * \n" +
				"\t * @param input Input image.  Modified.\n" +
				"\t * @param rand Random number generator\n" +
				"\t * @param mean Distribution's mean.\n" +
				"\t * @param sigma Distribution's standard deviation.\n" +
				"\t * @param lowerBound Lower bound of value clip\n" +
				"\t * @param upperBound Upper bound of value clip\n" +
				"\t */\n" +
				"\tpublic static void fillGaussian("+imageName+" input, Random rand , double mean , double sigma , "
				+sumType+" lowerBound , "+sumType+" upperBound ) {\n" +
				"\t\t"+dataType+"[] data = input.data;\n" +
				"\n" +
				"\t\tfor (int y = 0; y < input.height; y++) {\n" +
				"\t\t\tint index = input.getStartIndex() + y * input.getStride();\n" +
				"\t\t\tfor (int x = 0; x < input.width; x++) {\n" +
				"\t\t\t\t"+sumType+" value = "+castToSum+"(rand.nextGaussian()*sigma+mean);\n" +
				"\t\t\t\tif( value < lowerBound ) value = lowerBound;\n" +
				"\t\t\t\tif( value > upperBound ) value = upperBound;\n" +
				"\t\t\t\tdata[index++] = "+typeCast+"value;\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void printAddUniform() {

		String sumType = imageType.getSumType();
		int min = imageType.getMin().intValue();
		int max = imageType.getMax().intValue();
		String typeCast = imageType.getTypeCastFromSum();

		out.print("\t/**\n" +
				"\t * Adds uniform i.i.d noise to each pixel in the image.  Noise range is min <= X < max.\n" +
				"\t */\n" +
				"\tpublic static void addUniform("+imageName+" input, Random rand , "+sumType+" min , "+sumType+" max) {\n" +
				"\t\t"+sumType+" range = max-min;\n" +
				"\n" +
				"\t\t"+dataType+"[] data = input.data;\n" +
				"\n" +
				"\t\tfor (int y = 0; y < input.height; y++) {\n" +
				"\t\t\tint index = input.getStartIndex() + y * input.getStride();\n" +
				"\t\t\tfor (int x = 0; x < input.width; x++) {\n");
		if( imageType.isInteger() && imageType.getNumBits() != 64) {
			out.print("\t\t\t\t"+sumType+" value = (data[index] "+bitWise+") + rand.nextInt(range)+min;\n");
			if( imageType.getNumBits() < 32 ) {
				out.print("\t\t\t\tif( value < "+min+" ) value = "+min+";\n" +
						"\t\t\t\tif( value > "+max+" ) value = "+max+";\n" +
						"\n");
			}
		} else if( imageType.isInteger() ) {
			out.print("\t\t\t\t"+sumType+" value = data[index] + rand.nextInt((int)range)+min;\n");
		} else {
			String randType = imageType.getRandType();
			out.print("\t\t\t\t"+sumType+" value = data[index] + rand.next"+randType+"()*range+min;\n");
		}
		out.print("\t\t\t\tdata[index++] = "+typeCast+" value;\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void printAddGaussian() {
		String sumType = imageType.getSumType();
		String typeCast = imageType.getTypeCastFromSum();
		String sumCast = sumType.equals("double") ? "" : "("+sumType+")";

		out.print("\t/**\n" +
				"\t * Adds Gaussian/normal i.i.d noise to each pixel in the image.  If a value exceeds the specified\n"+
				"\t * it will be set to the closest bound.\n" +
				"\t * @param input Input image.  Modified.\n" +
				"\t * @param rand Random number generator.\n" +
				"\t * @param sigma Distributions standard deviation.\n" +
				"\t * @param lowerBound Allowed lower bound\n" +
				"\t * @param upperBound Allowed upper bound\n" +
				"\t */\n" +
				"\tpublic static void addGaussian("+imageName+" input, Random rand , double sigma , "
				+sumType+" lowerBound , "+sumType+" upperBound ) {\n" +
				"\n" +
				"\t\tfor (int y = 0; y < input.height; y++) {\n" +
				"\t\t\tint index = input.getStartIndex() + y * input.getStride();\n" +
				"\t\t\tfor (int x = 0; x < input.width; x++) {\n" +
				"\t\t\t\t"+sumType+" value = (input.data[index] "+bitWise+") + "+sumCast+"(rand.nextGaussian()*sigma);\n" +
				"\t\t\t\tif( value < lowerBound ) value = lowerBound;\n" +
				"\t\t\t\tif( value > upperBound ) value = upperBound;\n" +
				"\t\t\t\tinput.data[index++] = "+typeCast+" value;\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void printFlipVertical() {
		String sumType = imageType.getSumType();

		out.print("\t/**\n" +
				"\t * Flips the image from top to bottom\n" +
				"\t */\n" +
				"\tpublic static void flipVertical( "+imageName+" input ) {\n" +
				"\t\tint h2 = input.height/2;\n" +
				"\n" +
				"\t\tfor( int y = 0; y < h2; y++ ) {\n" +
				"\t\t\tint index1 = input.getStartIndex() + y * input.getStride();\n" +
				"\t\t\tint index2 = input.getStartIndex() + (input.height - y - 1) * input.getStride();\n" +
				"\n" +
				"\t\t\tint end = index1 + input.width;\n" +
				"\n" +
				"\t\t\twhile( index1 < end ) {\n" +
				"\t\t\t\t"+sumType+" tmp = input.data[index1];\n" +
				"\t\t\t\tinput.data[index1++] = input.data[index2];\n" +
				"\t\t\t\tinput.data[index2++] = ("+dataType+")tmp;\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void printFlipHorizontal() {
		String sumType = imageType.getSumType();

		out.print("\t/**\n" +
				"\t * Flips the image from left to right\n" +
				"\t */\n" +
				"\tpublic static void flipHorizontal( "+imageName+" input ) {\n" +
				"\t\tint w2 = input.width/2;\n" +
				"\n" +
				"\t\tfor( int y = 0; y < input.height; y++ ) {\n" +
				"\t\t\tint index1 = input.getStartIndex() + y * input.getStride();\n" +
				"\t\t\tint index2 = index1 + input.width-1;\n" +
				"\n" +
				"\t\t\tint end = index1 + w2;\n" +
				"\n" +
				"\t\t\twhile( index1 < end ) {\n" +
				"\t\t\t\t"+sumType+" tmp = input.data[index1];\n" +
				"\t\t\t\tinput.data[index1++] = input.data[index2];\n" +
				"\t\t\t\tinput.data[index2--] = ("+dataType+")tmp;\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void printRotateCW() {
		out.print("\t/**\n" +
				"\t * Rotates the image 90 degrees in the clockwise direction.\n" +
				"\t */\n" +
				"\tpublic static void rotateCW( "+imageName+" input , "+imageName+" output ) {\n" +
				"\t\tif( input.width != output.height || input.height != output.width )\n" +
				"\t\t\tthrow new IllegalArgumentException(\"Incompatible shapes\");\n" +
				"\n" +
				"\t\tint h = input.height-1;\n" +
				"\n" +
				"\t\tfor( int y = 0; y < input.height; y++ ) {\n" +
				"\t\t\tint indexIn = input.startIndex + y*input.stride;\n" +
				"\t\t\tfor (int x = 0; x < input.width; x++) {\n" +
				"\t\t\t\toutput.unsafe_set(h-y,x,input.data[indexIn++]);\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public void printRotateCCW() {
		out.print("\t/**\n" +
				"\t * Rotates the image 90 degrees in the counter-clockwise direction.\n" +
				"\t */\n" +
				"\tpublic static void rotateCCW( "+imageName+" input , "+imageName+" output ) {\n" +
				"\t\tif( input.width != output.height || input.height != output.width )\n" +
				"\t\t\tthrow new IllegalArgumentException(\"Incompatible shapes\");\n" +
				"\n" +
				"\t\tint w = input.width-1;\n" +
				"\n" +
				"\t\tfor( int y = 0; y < input.height; y++ ) {\n" +
				"\t\t\tint indexIn = input.startIndex + y*input.stride;\n" +
				"\t\t\tfor (int x = 0; x < input.width; x++) {\n" +
				"\t\t\t\toutput.unsafe_set(y,w-x,input.data[indexIn++]);\n" +
				"\t\t\t}\n" +
				"\t\t}\n" +
				"\t}\n\n");
	}

	public static void main( String args[] ) throws FileNotFoundException {
		GenerateImageMiscOps gen = new GenerateImageMiscOps();
		gen.generate();
	}
}
